<?php
namespace app\shop\api;
use app\common\api\BaseApi;
use app\shop\model\MemberConfig;
use app\system\model\UserGroup;
use app\system\api\UserGroupApi;
use app\system\api\UserMemberApi;

/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/9/11
 * Time: 11:43
 */
class MemberConfigApi extends BaseApi{

    const ADD             ="shop/MemberConfigApi/add";
    const GET_VIP         ="shop/MemberConfigApi/getVip";
    const GET_VIP_INFO    ="shop/MemberConfigApi/getVipInfo";
    const QUERY           ="shop/MemberConfigApi/query";
    const SAVE_BY_ID      ="shop/MemberConfigApi/saveByID";
    const SAVE            ="shop/MemberConfigApi/save";
    const QUERY_NO_PAGING ="shop/MemberConfigApi/queryNoPaging";
    const GET_INFO        ="shop/MemberConfigApi/getInfo";
    const SET_INC         ="shop/MemberConfigApi/setInc";
    const QUERY_ACCOUNT   ="shop/MemberConfigApi/queryAccount";

    protected function _init(){
        $this -> model = new MemberConfig();
    }

    /**
     * 根据id保存数据
     * @param $ID
     * @param $entity
     * @return bool|\Common\Api\status
     */
    public function saveByID($ID, $entity) {
        unset($entity['uid']);
        return $this -> save(['uid' => $ID], $entity);
    }
    /**
     * 获取用户VIP等级
     * 分VIP1，准VIP2，VIP2两种身份，无邀请码用户注册成为VIP1身份，有邀请码用户注册成为准VIP2用户，VIP1用户通过分享邀请其他十位用户注册可成为VIP2用户，准VIP2用户通过分享邀请其他5位用户激活成为会员。
     * @param  [type] $uid [description]
     * @return [type]      [description]
     */
    public function getVip($uid){
        //用户信息
        $r = $this->getInfo(['uid'=>$uid]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(empty($r['status'])) return $this->apiReturnErr('uid错误');
        // $userInfo = $r['info'];
        if(intval($r['info']['invite_id'])>0) $invite = 1;
        else $invite = 0;
        //查询用户组信息
        $r = apiCall(UserGroupApi::QUERY_NO_PAGING);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        $UserGroup2 = $r['info'];
        $UserGroup  = [];
        // trace('-----usergroup---');
        // trace($UserGroup);
        // return $this->apiReturnErr($UserGroup);
        foreach ($UserGroup2 as $v) {
            $UserGroup[$v['id']] = $v;
        }
        unset($UserGroup2);
        $r = apiCall(UserMemberApi::GET_INFO,[['uid'=>$uid]]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(!empty($r['info'])){
            $gid         = intval($r['info']['group_id']);
            $active      = intval($r['info']['active_status']);
            $active_time = $r['info']['active_time'];
            $r = $this->count(['invite_id'=>$uid]);
            if(!$r['status']) return $this->apiReturnErr($r['info']);
            $invite_num = intval($r['info']);
             if($active){
                $need   = 0;
             }else if($invite){
                $need = UserGroup::INVITE_GROUP_UP_NUMBER - $invite_num;
             }else{
                $need = UserGroup::DEFAULT_GROUP_UP_NUMBER - $invite_num;
             }
        }else{
            $active_time = time();
            if($invite){
                //邀请码注册
                //邀请过几人
                $r = $this->count(['invite_id'=>$uid]);
                if(!$r['status']) return $this->apiReturnErr($r['info']);
                $invite_num = intval($r['info']);
                if($invite_num<UserGroup::INVITE_GROUP_UP_NUMBER){
                    $gid    = UserGroup::INVITE_GROUP;
                    $active = 0;
                    $need   = UserGroup::INVITE_GROUP_UP_NUMBER - $invite_num;
                }else{
                    $gid    = UserGroup::SECOND_GROUP;
                    $active = 1;
                    $need   = 0;
                }
            }else{
                //邀请过几人
                $r = $this->count(['invite_id'=>$uid]);
                if(!$r['status']) return  $this->apiReturnErr($r['info']);
                $invite_num = intval($r['info']);
                if($invite_num<UserGroup::DEFAULT_GROUP_UP_NUMBER){
                    $gid    = UserGroup::DEFAULT_GROUP;
                    $active = 0;
                    $need   = UserGroup::INVITE_GROUP_UP_NUMBER - $invite_num;
                }else{
                    $gid = UserGroup::SECOND_GROUP;
                    $active = 1;
                    $need   = 0;
                }
            }
            $map = [
                'uid'           =>$uid,
                'group_id'      =>$gid,
                'active_status' =>$active,
                'active_time'   =>$active_time
            ];
            $r = apiCall(UserMemberApi::ADD,[$map]);
            if(!$r['status']) return  $this->apiReturnErr($r['info']);
        }
        $return = [
         'group_id'      =>''.$gid,
         'active_status' =>''.$active,
         'active_time'   =>''.$active_time,
         'group_title'   =>$UserGroup[$gid]['grouptitle'],
         'group_name'    =>$UserGroup[$gid]['groupdesc'],
         'up_need'       =>''.$need
        ];
        return $this->apiReturnSuc($return);
    }
    /**
     * 废弃 - 获取用户VIP等级
     * 分VIP1，准VIP2，VIP2两种身份，无邀请码用户注册成为VIP1身份，有邀请码用户注册成为准VIP2用户，VIP1用户通过分享邀请其他十位用户注册可成为VIP2用户，准VIP2用户通过分享邀请其他5位用户激活成为会员。
     * @param  [type] $uid [description]
     * @return [type]      [description]
     */
    // public function getVip($uid){
    //     $r = apiCall(UserMemberApi::GET_INFO,[['uid'=>$uid]]);
    //     if(!$r['status']) return '0';
    //     if(!empty($r['info'])){
    //         if(0 === intval($r['info']['active_status'])) return $r['info']['group_id'];
    //         else return '3';
    //     }
    //     $r = $this->getInfo(['uid'=>$uid]);
    //     if(!$r['status'] || empty($r['info'])) return '0';
    //     if($r['info']['invite_id']>0){
    //         //邀请码注册
    //         //邀请过几人
    //         $r = $this->count(['invite_id'=>$uid]);
    //         if(!$r['status']) return '0';
    //         if(intval($r['info'])<UserGroup::INVITE_GROUP_UP_NUMBER) return ''.UserGroup::INVITE_GROUP;
    //         return '3';
    //     }else{
    //         //邀请过几人
    //         $r = $this->count(['invite_id'=>$uid]);
    //         if(!$r['status']) return '0';
    //         if(intval($r['info'])<UserGroup::DEFAULT_GROUP_UP_NUMBER) return ''.UserGroup::DEFAULT_GROUP;
    //         return '2';
    //     }
    // }

    public function getVipInfo($uid){
        $r   = $this->getInfo(['uid'=>$uid]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(empty($r['info'])) return $this->apiReturnErr('uid 错误');

        $arr = ['invite_id'=>intval($r['info']['invite_id'])];
        $r   = $this->count(['invite_id'=>$uid]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        $arr['invite_num'] = intval($r['info']);
        return $this->apiReturnSuc($arr);
    }

    public function queryAccount($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
        $query = $this->model -> join("itboye_ucenter_member on itboye_member_config.uid=itboye_ucenter_member.id","left") -> join("common_member on itboye_member_config.uid=common_member.uid","left");
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> join("itboye_ucenter_member on itboye_member_config.uid=itboye_ucenter_member.id","left") -> join("common_member on itboye_member_config.uid=common_member.uid","left") -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));
    }

    /**
     *
     * 根据用户经验来设置用户组
     * @param $uid
     */
    public function setMGroup($uid,$store_id,$exp){


    }

}